package com.e0box.game.logicGraphics.nativeControl
{
	import com.e0box.game.logicGraphics.Options;
	import com.e0box.game.logicGraphics.data.PlayerData;
	
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import so.cuo.platform.admob.Admob;
	import so.cuo.platform.admob.AdmobEvent;
	import so.cuo.platform.admob.AdmobPosition;
	
	import starling.core.Starling;
	import starling.utils.RectangleUtil;
	import starling.utils.ScaleMode;

	public class AdManager
	{
		private static var _admob:Admob;
		
		private static var _shouldShow:Boolean;
		private static var _ready:Boolean;
		private static var _bannerSize:Point;
		private static var _alwaysShow:Boolean;
		
		private static var _tryToConnect:uint = 0;
		public function AdManager()
		{
			throw new Error("can't instance the static class 'AdManager'.");
		}
		
		public static function isSupport():Boolean{
			return (Admob.getInstance().supportDevice && Admob.getInstance().supportedBanner);
		}
		
		private static function init():void{
			_admob = Admob.getInstance();
			
			if(_admob.supportDevice && _admob.supportedBanner){
				
				if(Options.isIos){
					Admob.getInstance().setBannerKeys(Options.ADMOB_IOS_ID,Options.ADMOB_KEY);
				}else{
					Admob.getInstance().setBannerKeys(Options.ADMOB_ANDROID_ID,Options.ADMOB_KEY);
				}
				_admob.addEventListener(AdmobEvent.onBannerPresent, onBannerPresent);
				_admob.addEventListener(AdmobEvent.onBannerLeaveApplication, onBannerLeaveApplication);
				_admob.addEventListener(AdmobEvent.onBannerFailedReceive, onBannerFailedReceive);
				_admob.addEventListener(AdmobEvent.onBannerDismiss, onBannerDismiss);
				_admob.addEventListener(AdmobEvent.onBannerReceive, onBannerReceive);
				
				//_admob.showInterstitial();
				_admob.showBanner(Admob.BANNER, AdmobPosition.BOTTOM_CENTER);
				
				_bannerSize = new Point();
			}
		}
		
		protected static function onBannerDismiss(evt:AdmobEvent):void
		{
			trace(evt);
		}		
		
		protected static function onBannerFailedReceive(evt:AdmobEvent):void
		{
			trace(evt);
			
			_tryToConnect++;
			
			_admob.dispatchEvent(new Event("admobReady"));
		}
		
		protected static function onBannerLeaveApplication(evt:AdmobEvent):void
		{
			trace(evt);
		}
		
		protected static function onBannerPresent(evt:AdmobEvent):void
		{
			trace(evt);
		}
		
		protected static function onBannerReceive(evt:AdmobEvent):void
		{
			if(!_ready){
				_admob.hideBanner();
				_ready = true;
				_bannerSize.x = evt.data.width;
				_bannerSize.y = evt.data.height;
				
				if(Options.isIos){
					_bannerSize.x *= 2;
					_bannerSize.y *= 2;
				}
				
				getViewPort();
				
				trace("first receive:",_bannerSize);
			}
			
			if(!_shouldShow) hideBanner();
			
			_admob.dispatchEvent(new Event("admobReady"));
		}
		
		public static function getViewPort():Rectangle{
			var result:Rectangle;
			if(Starling.current){
				result = Starling.current.viewPort.clone();
			}else{
				result = RectangleUtil.fit(
					new Rectangle(0,0,Options.STAGE_WIDTH, Options.STAGE_HEIGHT),
					new Rectangle(0, 0, Options.fullScreenWidth, Options.fullScreenHeight), 
					ScaleMode.SHOW_ALL, Options.isIos);
			}
			
			_alwaysShow = (Options.fullScreenHeight - result.height >= _bannerSize.y) && _bannerSize.y;
			
			if(_alwaysShow){
				if(result.y >= _bannerSize.y){
					
				}else if(result.y >= _bannerSize.y / 2){
					result.y -= _bannerSize.y / 2;
				}
			}
			
			return result;
		}
		
		public static function showBanner():void{
			if(PlayerData.instance.isCharge) return;
			
			if(_tryToConnect > 3){
				return;
			}
			
			if(!_admob) init();
			
			_admob.showBanner(Admob.BANNER, AdmobPosition.BOTTOM_CENTER);
			
			if(!_ready) return;
			
			_shouldShow = true;
		}
		
		public static function hideBanner():void{
			if(!_admob) init();
			
			_admob.hideBanner();
			
			if(!_ready) return;
			
			_shouldShow = false;
			
		}
		
		public static function get admob():Admob{
			if(!_admob) init();
			
			return _admob;
		}
		
		public static function get alwaysShow():Boolean{
			return _alwaysShow;
		}
		
	}
}